package response

type RespCode int

// type RespMsg string
type RespData interface{}

// Response 定义业务处理响应类型
type Response struct {
	Code RespCode `json:"code"`
	Msg  string   `json:"msg"`
	Data RespData `json:"data"`
}

func (resp *Response) SetData(data RespData) *Response {
	resp.Data = data
	return resp
}

func (resp *Response) SetMsg(msg string) *Response {
	resp.Msg = msg
	return resp
}

func (resp *Response) SetCode(code RespCode) *Response {
	resp.Code = code
	return resp
}

func (resp *Response) SetCodeAndMsg(code RespCode) *Response {
	resp.Code = code
	resp.Msg = CodeToText(code)
	return resp
}

// 通用返回
func NewResp(code RespCode, data RespData, msg string) *Response {
	return &Response{
		Code: code,
		Data: data,
		Msg:  msg,
	}
}

// 默认成功返回
func NewSuccessResp() *Response {
	return &Response{
		Code: RespCodeOK,
		Msg:  CodeToText(RespCodeOK),
	}
}

// 默认失败返回
func NewFailResp() *Response {
	return &Response{
		Code: RespFailed,
		Msg:  CodeToText(RespFailed),
	}
}

// 通过code获取msg
func CodeToText(code RespCode) string {
	if text, ok := responseText[code]; ok {
		return text
	}
	return ""
}
